package edu.ccut.saturn.manager.bundles.core;

import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;

public class GetSubList extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		
		List<?> list = (List<?>)parameterContext.getParameterValue(0);
		int start, limit;
		try{
			start = (Integer)parameterContext.getParameterValue(1);
			limit = (Integer)parameterContext.getParameterValue(2);
		} catch (Exception e) {
			start = 0;
			limit = 10;
		}
		
		List<Object> result = new ArrayList<Object>();
		
		int size = list.size();
		
		int end = start + limit > size ? size : start + limit;
		
		for(int i = start; i < end; i ++){
			result.add(list.get(i));
		}
		
		parameterContext.setParameter(3, result);
		parameterContext.setParameter(4, size);
	}
}